var mongoose = require('mongoose');
var Upload = require('../models/upload-model');
//对文件操作的模块
var multer = require('multer');
var path = require('path');
//文件操作模块
var fs = require('fs');



// 图片的上传
var storage=multer.diskStorage({
    destination:function(req,file,cb){
        cb(null,'./uploads');
    },
    filename:function(req,file,cb){
        const imageNameIndex = file.originalname.indexOf('.');
        const imageName = file.originalname.substring(0,imageNameIndex);

        cb(null,file.fieldname+'-'+imageName+'-'+Date.now()+path.extname(file.originalname))
    }
})

var upload=multer({storage:storage})

exports.upload=function(req,res,next){
    var upload=multer({
        storage:storage,
        fileFilter:function(req,file,callback){
            var ext=path.extname(file.originalname);
            if(ext !=='.png' && ext !=='.jpg' && ext !=='.gif' ){
                return callback(res.end('文件类型不符'));
            }
            callback(null,true);
        }
    }).single('avatar')
    upload(req,res,function(err){   
        var upload=new Upload(req.file);
        upload.save()
        .then(data=>{
            res.json(data);
        });
        // res.end('file uploaded');
    });
}

//查找
exports.list = function(req,res,next){
    var page = (req.body.page) ? req.body.page : 1;
    var rows = (req.body.rows) ? req.body.rows : 10;
    rows = parseInt(rows);
    var queryCondition = {};
    var name;
    if( req.body.name && req.body.name.trim().length>0 ){
        name = req.body.name;
        queryCondition = {"mimetype":new RegExp(name,'i')};
    }

    if(req.body.classifyID && req.body.classifyID.trim().length>0){
        classifyID=req.body.classifyID;
        queryCondition={
            "classifyID":classifyID 
        }
    }
    //对查找到的数据进行分页
    Upload.paginate(queryCondition, { page: page, limit: rows }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
       res.json(result)
    });
}


//数据库删除
exports.remove = function(req,res,next){
    var id = req.params.id;   //此时id是传过来的值
    id = id.split(',');   //将传递的id值变成数组遍历
    let leng = id.length;
    for(var i=0;i<leng;i++){
        Upload.findByIdAndRemove(id[i],function(err,doc){
            // if(err){
            //     console.log("数据删除异常"+err);
            //     return;
            // }else{
            //     res.json({msg:'数据删除成功',status:200})
                
            // }
        })
    }
    res.send();
    //对这个图片文件进行删除
} 

exports.removefile = function(req,res,next){
    console.log(req.body.url)
    if(req.body.url == undefined) {
        for (var index = 0; index < req.body.length; index++) {
            fs.unlink("uploads/"+req.body[index],function (err) {
                if (err) {
                    console.log('出错'+err)
                } else {
                    console.log('成功')
                    
                }
            })
        }
    }else{
        fs.unlink("uploads/"+req.body.url,function (err) {
            if (err) {
                console.log('出错'+err)
            } else {
                console.log('成功')
            }
        })
    }

    

}
